package net.chasing.retrofit.bean.base;

import net.chasing.retrofit.RetrofitConstant;
import net.chasing.retrofit.util.MobileInfo;
import net.chasing.retrofit.util.ObjectUtils;
import net.chasing.retrofit.util.RetrofitLog;
import net.chasing.retrofit.util.encode.CodeUtils;

import java.util.LinkedHashMap;
import java.util.UUID;

/**
 * POST请求 Body标签 使用的参数，仅进行请求头的封装
 * 不需要传递请求头的可以不需要进行继承使用
 */
public abstract class BaseReq {

    public String sign;// 字符串	签名
    public long timestamp;// 32位无符号整型	时间戳
    public String echostr;// 字符串	32位随机字符串
    public String driver_code;// 字符串	设备唯一ID
    public String platform;// 字符串	平台，win,android,ios,web,mac
    public String app_version;// 字符串	软件版本号
    public String channel;// 软件的推广渠道。由英文小写字母、数字组成，最大长度不超过30个字符。渠道不存在应设定为default
    public String appid; // 字符串  超级兔子便签清单的APPID是：cjtz.note.win   cjtz.note.ios      cjtz.note.android

    public BaseReq(){
        timestamp = System.currentTimeMillis();
        echostr = UUID.randomUUID().toString().toLowerCase().replace("-", "");
        driver_code = (String) MobileInfo.getInstance().getDeviceInfo().get(MobileInfo.ANDROID_ID);
        platform = "android";
        appid = RetrofitConstant.APP_ID;
        channel = RetrofitConstant.CHANNEL;
        app_version = RetrofitConstant.VERSION_NAME;
    }

    public void sign() {
        LinkedHashMap<String, Object> signMap = new ObjectUtils().toMapWithOrder(this);
        signMap.remove("sign");
        String[] afterOrder = signMap.keySet().toArray(new String[0]);
        StringBuilder sb = new StringBuilder();
        for (String item : afterOrder) {
            sb.append(item).append("=").append(signMap.get(item)).append("&");
        }
        sb.append("key=7c0a5076038849a50d86fba336b074a3");
        String signature = sb.toString();
        RetrofitLog.i("retrofit request signature:" + signature);
        signature = CodeUtils.sha1(signature);
        sign = signature;
        RetrofitLog.i("retrofit request sign:" + sign);
    }
}
